
/* Generated by Interface Builder */

#import "EyeView.h"
#import "eye.h"
#import <math.h>
#import <dpsclient/dpsclient.h>
#import <dpsclient/psops.h>

#define MAX_BALL_RAD	9.0
#define PUPIL_RAD	2.0
#define GAP		3.0

int square(int x)
{
    return (x * x);
}

@implementation EyeView

+ newFrame:(const NXRect *) frm;
{
    [(self = [super newFrame:frm]) allocateGState];
    return self;
}

-init
{
    leftCenter.x  = bounds.size.width / 4;
    rightCenter.x = bounds.size.width * 3 / 4;
    leftCenter.y = rightCenter.y = bounds.size.height / 2;

    // draw the eye background
    [self lockFocus];
    PSsetgray(NX_LTGRAY);
    PSrectfill(0.0, 0.0, bounds.size.width, bounds.size.height);
    PSsetgray(0.0);
    PSsetlinewidth(2);
    PSarc(leftCenter.x, leftCenter.y, MAX_BALL_RAD + PUPIL_RAD + GAP, 0.0, 360.0);
    PSstroke();
    PSarc(rightCenter.x, rightCenter.y, MAX_BALL_RAD + PUPIL_RAD + GAP, 0.0, 360.0);
    PSstroke();
    [self unlockFocus];
    [window flushWindow];
    NXPing();
}

- drawBalls
{
    mouseTemp = mouseLoc;
    [window getMouseLocation: &mouseLoc];
    [self convertPoint: &mouseLoc fromView: nil];
    if (mouseLoc.x == mouseTemp.x && mouseLoc.y == mouseTemp.y) return;

    mag = sqrt(square(mouseLoc.x - leftCenter.x) + square(mouseLoc.y - leftCenter.y));
    if (mag < MAX_BALL_RAD) {
    	leftBall.x = mouseLoc.x;
    	leftBall.y = mouseLoc.y;
    } else {
    	leftBall.x = MAX_BALL_RAD * (mouseLoc.x - leftCenter.x) / mag + leftCenter.x ;
    	leftBall.y = MAX_BALL_RAD * (mouseLoc.y - leftCenter.y) / mag + leftCenter.y ;
    }
    mag = sqrt(square(mouseLoc.x - rightCenter.x) + square(mouseLoc.y - rightCenter.y));
    if (mag < MAX_BALL_RAD) {
    	rightBall.x = mouseLoc.x;
    	rightBall.y = mouseLoc.y;
    } else {
    	rightBall.x = MAX_BALL_RAD * (mouseLoc.x - rightCenter.x) / mag + rightCenter.x;
    	rightBall.y = MAX_BALL_RAD * (mouseLoc.y - rightCenter.y) / mag + rightCenter.y;
    }
    
    [self lockFocus];
    eyeball(leftCenter.x, leftCenter.y, rightCenter.x, rightCenter.y, leftBall.x, leftBall.y, rightBall.x, rightBall.y, MAX_BALL_RAD, PUPIL_RAD, GAP);
    [window flushWindow];
    [self unlockFocus];
    NXPing();
}

@end
